package com.cnc.help.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

import com.cnc.bas.web.SecureBaseDispatchAction;

/**
 * 显示帮助系统页面action
 * @author Administrator
 *
 */
public class HelpAction  extends SecureBaseDispatchAction {
    
    // 下载的文档
    private static final String DL_FILE_MANUAL = "01";   //网宿大文件同步系统客户操作指南
    
    // 帮助文档文件路径
    private static final String MANUAL_FILE_PATH = "/doc/manual.pdf";
    
    
    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
    }
    
    /**
     * 显示系统使用手册页面
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward showManual(ActionMapping mapping,ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception {       
         return mapping.findForward("showManual");
     }
    
    /**
     * 显示常见问题
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward download(ActionMapping mapping,ActionForm form,HttpServletRequest request,
            HttpServletResponse response) throws Exception { 
        
        String downloadFile = request.getParameter("dlFile");
        if(StringUtils.isEmpty(downloadFile)){
            return null;
        }
        
        String fileName = "";
        if(DL_FILE_MANUAL.equals(downloadFile)){
            fileName = MANUAL_FILE_PATH;
        }
        
        if(StringUtils.isEmpty(fileName)){
            return null;
        }
 
        // 获取绝对路径
        String filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/");
       
        String fileFullName = filePath + "/" +  fileName;
        FileInputStream fin = new FileInputStream(fileFullName);

        //  是否允许直接在浏览器内打开（如果浏览器能够预览此文件内容）
        boolean isInline = false;

        response.reset();

        try {
            if (fin != null) {
                //  从服务器的配置来读取文件的 contentType 并设置此contentType, 不推荐设置为
                //  application/x-download, 因为有时候我们的客户可能会希望在浏览器里直接打开,
                //  如 Excel 报表, 而且 application/x-download 也不是一个标准的 mime type,
                //  似乎 FireFox 就不认识这种格式的 mime type
                String mimetype = null;
                mimetype = request.getSession().getServletContext().getMimeType(filePath);
                if (mimetype == null) {
                    mimetype = "application/octet-stream;charset=iso8859-1";
                }
                response.setContentType(mimetype);
                //  IE 的话就只能用 IE 才认识的头才能下载 HTML 文件, 否则 IE 必定要打开此文件!
                String ua = request.getHeader("User-Agent"); //  获取终端类型
                if (ua == null)
                    ua = "User-Agent:Mozilla/4.0(compatible; MSIE 6.0;)";
                boolean isIE = ua.toLowerCase().indexOf("msie") != -1; //  是否为 IE
                if (isIE && !isInline) {
                    mimetype = "application/x-msdownload";
                    response.setContentType(mimetype);
                }
                //  下面我们将设法让客户端保存文件的时候显示正确的文件名, 具体就是将文件名
                //  转换为 ISO8859-1 编码
                String realName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
                
                String downFileName = new String(realName.getBytes(), "iso8859-1");

                String inlineType = isInline ? "inline" : "attachment"; //  是否内联附件
                
                response.setHeader("Content-Disposition", inlineType + ";filename=\"" + downFileName + "\"");

                byte[] buffer = new byte[4096]; //  缓冲区
                BufferedOutputStream output = null;
                FileInputStream input = null;
                int length = 0;
                try {
                    output = new BufferedOutputStream(response.getOutputStream());
                    input = fin;
                    int n = -1;
                    while ((n = input.read(buffer, 0, 4096)) > -1) {
                        output.write(buffer, 0, n);
                        length += n;
                    }
                    response.setContentLength((int) length); //  设置下载内容大小
                    response.flushBuffer();
                } catch (Exception e) {
                } //  用户可能取消了下载
                finally {
                    if (input != null)
                        input.close();
                    if (output != null)
                        output.close();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //  如果下载失败了就告诉用户此文件不存在
        try {
            response.sendError(404);
        }
        catch (Exception e) {
        }
        return null;
    }
    
}
